var express = require('express');
var router = express.Router();

// 创建mysql实例
const CreateQuery = require("./mysql")
const create = new CreateQuery()

function uuid() {
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
/* GET home page. */
//获取全部数据
router.get('/getData', function (req, response, next) {
  // 连接mysql 
  create.init_database()
  create.select(`select * from list`).then((res) => {
    // console.log(res);
    // console.log(res);
    response.send(res)
  })
  create.destory()
});
//获取购物车数据
router.get('/shopList', function (req, response, next) {
  // 连接mysql 
  create.init_database()
  create.select(`select * from cardata`).then((res) => {
    // console.log(res);
    response.send(res)
  })
  create.destory()
});

//添加数据
router.post('/addList', function (req, response) {
  let {
    id,
    img,
    sed,
    name,
    price,
    oldPrice,
    label,
    num
  } = req.body;
  console.log(req.body);
  create.init_database();
  // 获取cardata数据
  create.select(`select * from cardata`).then((res) => {
    let index = res.findIndex(item => {
      return item.id === id
    })
    if (index !== -1) {
      create.init_database();
      let Num = Number(res[index].num)
      // console.log(typeof Number(num));
      create.select(`update cardata set num = '${Num + Number(num)}' where id='${res[index].id}'`)
      // create.destory()
      return
    } else {
      create.init_database();
      let sql = `insert into cardata(id,img,isCheck,shopName,price,oldPrice,label,num)
       values('${id}','${img}','${sed}','${name}','${price}','${oldPrice}','${label}','${num}')`
      create.select(sql)
      // create.destory()
    }
  })
  create.destory()
  response.send()
})


//减减的 删除哪那一项
router.post('/listRem', function (req, response, next) {

  let {
    id
  } = req.body
  create.init_database()

  create.select(`DELETE from cardata where id='${id}'`).then((res) => {
    response.send(res)
  })
  create.destory()
});

//全部删除
router.post('/remList', function (req, response, next) {
  // console.log(req.body);
  create.init_database()
  req.body.forEach((item) => {
    create.select(`DELETE FROM cardata WHERE id='${item}'`).then((res) => {
      console.log(res);
      response.send(res)
    })
  })
  create.destory()
});

//加加
router.post('/addBtnList', function (req, response, next) {
  //UPDATE <表名> SET 字段 1=值 1 [,字段 2=值 2… ] [WHERE 子句 ]
  let {
    id,
    num
  } = req.body
  create.init_database()
  create.select(`UPDATE  cardata  SET num='${num}' WHERE  id='${id}'`).then((res) => {
    response.send(res)
  })
  create.destory()
});

//减减

router.post('/jianjian', function (req, response, next) {
  let {
    id,
    num
  } = req.body
  create.init_database()
  create.select(`UPDATE  cardata  SET num='${num}' WHERE  id='${id}'`).then((res) => {
    response.send(res)
  })
  create.destory()
});
router.get('/Homedata', function (req, response) {
  create.init_database()
  create.select(`select * from list`).then(res => {
    console.log(res);
    response.send(res)
  })
  create.destory()
})

// 添加购物车
router.post('/addShopCarData', function (req, response) {
  console.log(req.body);
  let {
    id,
    img,
    isCheck,
    shopName,
    price,
    oldPrice,
    label,
    num
  } = req.body;
  create.init_database();
  // 获取cardata数据
  create.select(`select * from cardata`).then((res) => {
    let index = res.findIndex(item => {
      return item.id === id
    })
    console.log(index);
    if (index !== -1) {
      create.init_database();
      let Num = Number(res[index].num)
      create.select(`update cardata set num='${Num + num}' where id='${res[index].id}'`)
      // create.destory()
      return
    } else {
      create.init_database();
      let sql = `insert into cardata(id,img,isCheck,shopName,price,oldPrice,label,num,state) 
      values('${id}','${img}','${isCheck}','${shopName}','${price}','${oldPrice}','${label}','${num}','${state=1}')`
      create.select(sql)
      // create.destory()
    }
  })
  create.destory()
  response.send('添加成功')
})
// 结算购物车数据
router.post('/addOrder', function (req, response, next) {
  create.init_database();
  // 获取cardata数据
  create.select(`select * from carOrder`).then((res) => {
    req.body.forEach(ite => {
      let index = res.findIndex(item => {
        return item.id === ite.id
      })
      console.log(index);
      if (index !== -1) {
        create.init_database();
        let Num = Number(res[index].num)
        create.select(`update carOrder set num='${Num + Number(ite.num)}' where id='${res[index].id}'`)
        // create.destory()
        return
      } else {
        create.init_database();
        let sql = `insert into carOrder(id,img,shopName,price,num) values('${ite.id}','${ite.img}','${ite.shopName}','${ite.price}','${ite.num}')`
        create.select(sql)
        // create.destory()
      }
    })
  })
  // create.destory()

  // 删除对应商品库存
  create.select(`select * from list`).then((resp) => {
    req.body.forEach(ite => {
      let index = resp.findIndex(item => {
        return item.id === ite.id
      })
      create.select(`update list set inventoryNum='${resp[index].inventoryNum - ite.num}' where id='${ite.id}'`)
    })
    

  })
  create.destory()

  response.send('结算成功')
});

module.exports = router;